﻿@page "/search"

@if (Parameters != null)
{
    foreach (var p in Parameters)
    {
        if (p.Name == "出发时间")
        {
            p.DateValue = SetParameterDefaultValue<DateTime?>(p.DateValue, p.Name);
            <div>@p.Name 
                <DateTimePicker @bind-Value="p.DateValue" />
            </div>
        }else if (p.Name == "危险")
        {
            p.BooleanValue = SetParameterDefaultValue<bool?>(p.BooleanValue, p.Name);
            <div>
                @p.Name
                <NullCheckbox TValue="bool?" @bind-Value="p.BooleanValue" />
            </div>
        }
    }
    FirstRender = false;
}

<Select Items="Items" TValue="string" IsPopover IsVirtualize="true"></Select>

<Textarea rows="3" class="h-100" @bind-Value="Value" />

 @code {
    public class Reports
    {
        public string? Name{ get; set; }
        public DateTime? DateValue{ get; set; }
        public bool? BooleanValue{ get; set; }

        public object? ParameterValue{ get; set; }

        public void IntegrateParamterValue()
        {
            if (DateValue.HasValue)
                ParameterValue = DateValue.Value.Date;
            else if (BooleanValue.HasValue)
                ParameterValue = BooleanValue.Value;
            else
                ParameterValue = null;
        }
    }

    public List<Reports>? Parameters{ get; set; }

    public string? Value{ get; set; }

    public EventCallback<string?> ValueChanged{ get; set; }

    bool FirstRender = true;

    public List<SelectedItem>? Items{ get; set; }

    protected T SetParameterDefaultValue<T>(T v, string name)
    {
        if (v != null)
            return v;
        object? value = null;
        if (FirstRender)
        {
            if (name == "出发时间")
            {
                value = DateTime.Now.AddDays(-1);
            }
            else if (name == "危险")
            {
                value = true;
            }
        }
        return value!.ToValue<T>();
    }

    protected override void OnInitialized()
    {
        Value = "a\r\nb";
        Items = new List<SelectedItem>()
        {
            new SelectedItem(){ Text="A", Value="A"},
            new SelectedItem(){ Text="B", Value="B"},
        };

        Parameters = new List<Reports>()
        {
            new Reports(){Name="出发时间"},
            new Reports(){Name="危险"}
        };
    }
}
